/* Copyright (C) 2011. ChengDu Unicon system, All Rights Reserved */

/*""FILE COMMENT""******************************* Technical reference data ****
* File Name	: pwm.c
* Version 	: 1.00
* Device 	: SH72543
* Tool Chain 	: 
* H/W Platform	: 
* Description 	: Timer E control
******************************************************************************/

/******************************************************************************
* History 	: 01.06.2011 Ver. 1.00 First Release
*		: 
*FILE COMMENT END*********************************************************/

#include <include.h>
/* Define PWM channel index  
 *                                        PWM1  PWM2  PWM3  PWM4  PWM5  PWM6  PWM8  PWM9  */
const CPU_INT08U PWM_CHANNEL_INDEX[] = {0x30, 0x31, 0x32, 0x33, 0x00, 0x01, 0x02, 0x03, };

st_pwmSet pwmSetValue[ MAX_PWM_CHAN ] = {0};

/**************************************************************************
 *Function   : AuxPwmUpdata       
 *Description:                                                     
 *Input      :                             
 *Output     :                              
 *Return     : 
 *G Variable :                              
 *Notes      :                                   
 **************************************************************************/
void AuxPwmUpdata( const st_pwmSet *const pwmSetValue )
{
  CPU_INT16U cycleTemp  = 0;
  CPU_INT08U blockNum   = 0;
  CPU_INT08U channelNum = 0;
  
  blockNum   = (PWM_CHANNEL_INDEX[ pwmSetValue->channel - 1 ] >> 4) & 0x0F;
  channelNum = PWM_CHANNEL_INDEX[ pwmSetValue->channel - 1 ] & 0x0F;
  
  /* Cycle Register Value = (E Frequre/Cycle Set Value) */
  cycleTemp    = PWM_BUS_CLOCK / pwmSetValue->pwmCycle;
  ATUE.SUBBLOCK[blockNum].CRLDE[channelNum] = cycleTemp;
  
  if ( TRUE == pwmSetValue->pwmStart )
  {
    /* Duty Register Value = (Cycle/100.00%)*Duty Set Value */
    ATUE.SUBBLOCK[blockNum].DRLDE[channelNum] = (cycleTemp / 10000) * pwmSetValue->pwmDuty;
  }
  else
  {
    ATUE.SUBBLOCK[blockNum].DRLDE[channelNum] = 0;
  }
  
  ATUE.SUBBLOCK[blockNum].SSTRE.BYTE |= ((CPU_INT08U)pwmSetValue->pwmStart) << channelNum; 
}

/**************************************************************************
 *Function   :        
 *Description:                                                     
 *Input      :                              
 *Output     :                              
 *Return     : 
 *G Variable :                              
 *Notes      :                                   
 **************************************************************************/
void TimerE_Init()
{
  CPU_INT08U loopCnt1 = 0;
  CPU_INT08U loopCnt2 = 0;
  /* The timer E counnter run When 
     the timer E enable bit(TEE) in the ATU-III master enable register(ATUENR),
     timer Start register E(TSTRE),
     and subblock starting register E(SSTRE) must be set. */ 

  ATUCTRL.ATUENR.BIT.TEE = 1; //Timer E counter operation enabled
  
  ATUE.TSTRE.BIT.STRE3   = 1; //Timer Start Register E
  ATUE.TSTRE.BIT.STRE2   = 1;
  ATUE.TSTRE.BIT.STRE0   = 1;
  
  for( loopCnt1=0; loopCnt1<4; loopCnt1++ )
  {
    /* Timer Control Register, Select the counter clock of prescaler E from clock-bus lines 0 to 5 */
    ATUE.SUBBLOCK[loopCnt1].TCRE.BIT.CKSELE  = 0; /* Clock-bus line 0 */
  
    /* Timer Output Control Register, 0:Signal is output as is on PWM output pin
     *                                1:Inverted signal is output on PWM output pin */
    ATUE.SUBBLOCK[loopCnt1].TOCRE.BYTE     = 0;   
    ATUE.SUBBLOCK[loopCnt1].TIERE.BYTE     = 0; //Timer Interrupt Enable Register
    ATUE.SUBBLOCK[loopCnt1].RLDCRE.BYTE    = 1; //Reload Control Register
    ATUE.SUBBLOCK[loopCnt1].TSRE.BYTE      = 0; //Timer Status Register
    ATUE.SUBBLOCK[loopCnt1].PSCRE.BIT.PSCE = 3; //Prescaler Register E, From H'0 to H'7 
    ATUE.SUBBLOCK[loopCnt1].SSTRE.BYTE     = 0;
    
    for( loopCnt2=0; loopCnt2<4; loopCnt2++ )
    {
      ATUE.SUBBLOCK[loopCnt1].TCNTE[loopCnt2] = 0; //Clear Timer Counter E 
    }
  }
#if TEST_PIN_EN == 1 /* Define PORTL #5 as test pin  */
  PORTL.CR1.BIT.MD5  = 0; // As IO
  PORTL.DR.BIT.DR5   = 0; // Pin Data Reg = 0
  PORTL.IOR.BIT.IOR5 = 1; // Output
#else
  PORTL.CR1.BIT.MD5 = 1; // Enable PWM1 Out Pin
#endif
  
  PORTL.CR1.BIT.MD6 = 1; // Enable PWM2 Out Pin
  PORTL.CR1.BIT.MD7 = 1; // Enable PWM3 Out Pin
  PORTL.CR2.BIT.MD8 = 1; // Enable PWM4 Out Pin
  
  PORTE.CR1.BIT.MD6 = 1; // POE00, Enable PWM5 Out Pin
  PORTE.CR1.BIT.MD7 = 1; // POE01,
  PORTE.CR2.BIT.MD8 = 1; // POE02,
  PORTE.CR2.BIT.MD9 = 1; // POE03,
  
  PORTL.CR1.BIT.MD1 = 1; //POE20, 
  PORTL.CR1.BIT.MD2 = 1; //POE21,
  PORTL.CR1.BIT.MD3 = 1; //POE22,
  PORTL.CR1.BIT.MD4 = 1; //POE23,
}